package com.lcz.wanandroid.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lcz.wanandroid.R;
import com.lcz.wanandroid.activity.login.LoginBean;
import com.lcz.wanandroid.activity.login.LoginP;
import com.lcz.wanandroid.activity.login.LoginV;
import com.lcz.wanandroid.base.BaseActivity;
import com.lcz.wanandroid.utils.SpUtil;
import com.lcz.wanandroid.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginV, LoginP> implements LoginV {


    @BindView(R.id.img_return)
    ImageView imgReturn;
    @BindView(R.id.et_login)
    EditText etLogin;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_register)
    Button btnRegister;
    private String mName;
    private String mPsw;
    private Intent mIntent;

    @Override
    public void onSussion(LoginBean bean) {

    }

    @Override
    protected void initView() {
        super.initView();
        String lczname = (String) SpUtil.getParam("lczname", "");
        boolean login = (Boolean) SpUtil.getParam("login", false);
        if(login){
            etLogin.setText(lczname);
            etPsw.setText("******");
        }
        mIntent = getIntent();
    }

    @Override
    protected void initData() {
        super.initData();
    }


    @Override
    protected LoginP Inpresneter() {
        return new LoginP();
    }

    @Override
    protected int LayoutId() {
        return R.layout.activity_login;
    }


    @OnClick({R.id.img_return, R.id.btn_login, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                finish();
                break;
            case R.id.btn_login:
                login();
//                ToastUtil.showShort("系统更新中... 暂时停用登陆服务 \r\n \r\n 别着急正在努力更新中 (*❦ω❦)");
                break;
            case R.id.btn_register:
//                ToastUtil.showShort("系统更新中... 暂时停用注册服务 \r\n \r\n 别着急正在努力更新中 (*❦ω❦)");
                Intent intent = new Intent(LoginActivity.this, ResgisterActivity.class);
                startActivityForResult(intent, 100);
                break;
        }
    }

    private void login() {
        mName = etLogin.getText().toString().trim();
        mPsw = etPsw.getText().toString().trim();
        if (!TextUtils.isEmpty(mName) && !TextUtils.isEmpty(mPsw)) {
            SpUtil.setParam("lczname", mName);
            SpUtil.setParam("login", true);
            mIntent.putExtra("uselogin", mName);
            setResult(7777, mIntent);
            finish();
        } else {
            ToastUtil.showShort("您输入的账号密码不能为空");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            String use = data.getStringExtra("use");
            String psw = data.getStringExtra("psw");
            etLogin.setText(use);
            etPsw.setText(psw);
        }
    }
}
